<?php
include 'functions.php';
include 'common.php';

// get user's input csv
$userInput = $_GET["csv"];
$inputcsv = getCSVfromString($userInput, true);
$in_header = $inputcsv[$HEADER];
$in_headerIndex = $inputcsv[$HEADER_INDEX];
$in_body = $inputcsv[$BODY];

// get the user's parameters
$parameters = getParameters($_GET["p"]);
$locType = $_GET["locType"];
$locIndex1 = $_GET["locIndex1"];
$locIndex2 = $_GET["locIndex2"];
$dateType = $_GET["dateType"];
$dateIndex = $_GET["dateIndex"];
$dateFormat = $_GET["dateFormat"];

// final output
$output = '';

// print original headers with newly integrated columns
$numHeaders = count($in_header);
for($i = 0; $i < $numHeaders; $i++) {
    $output .= $in_header[$i] . ',';
}

// add new columns
$output .= printParameters($parameters) . $NEWLINE; // mean weather temperature

$log = '';  // in lieu of an actual log, this just keeps a giant string to print to the screen

// print the body of the new csv
for($row = 0; $row < count($in_body); $row++) {
    if(strcmp($locType, $ZIPCODE) == 0) {   // we were given the zipcode
        $zipcode = $in_body[$row][$locIndex1];
    } else {    // given the lat/lon, get the zipcode from wunderground
        // saves the weather underground xml data as a local file
        $lat = $in_body[$row][$locIndex1];
        $lon = $in_body[$row][$locIndex2];
        $weatherXMLstring = getXML($lat, $lon);

        // parse the xml to get the zipcode
        $weatherxml = simplexml_load_string($weatherXMLstring);
        $zipcode = $weatherxml->zip;
    }
        
    // parse the date
    $date = date_parse($in_body[$row][$dateIndex]);
    $year = $date[year];
    $month = $date[month];
    $day = $date[day];

    // get the historical data csv
    $historyurl = "http://www.wunderground.com/history/zipcode/" . $zipcode . "/" . $year . "/" . $month . "/" . $day . "/WeeklyHistory.html?format=1";
    if(strcmp($zipcode, '00000') != 0) {
        $historycsv = getCSV($historyurl, true);
        $weather_header = $historycsv[$HEADER];
        $weather_headerIndices = $historycsv[$HEADER_INDEX];
        $weather_body = $historycsv[$BODY];
    }
    
    //$log .= '<a href="' . $historyurl . '">' . $historyurl . '</a><br/>';  // DEBUG

    // print row of the original csv
    $num = count($in_body[$row]);
    for($i = 0; $i < $num; $i++) {
        $output .= $in_body[$row][$i] . ',';
    }

    if(strcmp($zipcode, '00000') != 0) {
        for($i = 0; $i < count($parameters); $i++) {
            // add new columns
            $index = strtolower($parameters[$i]);
            $weather_row = getWeatherRowFromDate($weather_body, $month, $day, $year);   // TODO get row that corresponds to correct date
            $output .= $weather_body[$weather_row][$weather_headerIndices[$index]];
            
            if($i < count($parameters) - 1) {
                $output .= ",";
            }
        }
    }
    
    if($row < count($in_body)-1) {
        // end of this row
        $output .= $NEWLINE;
    }
}

$output = trim($output); // get rid of trailing whitespace

echo($output);

// FOR DEBUGGING
//echo '<br/><br/><p>(DEBUGGING) Data used:</p>';
//echo '<p>' . $log . '</p>';
// END DEBUGGING

?>

